package com.pqtest.api.controller;

import com.pqtest.api.annotation.Paging;
import com.pqtest.api.annotation.ResponseFormat;
import com.pqtest.api.dto.SlSimulibraryAnswerDto;
import com.pqtest.api.dto.SlSimulibrarySubmitDto;
import com.pqtest.api.dto.SlSimulibraryUpdatetimeDto;
import com.pqtest.api.service.SlSimuService;
import com.pqtest.api.service.SllibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
public class SllibraryController extends BaseController{
    @Autowired
    SllibraryService sllibraryService;
    @Autowired
    SlSimuService slSimuService;
    @GetMapping("/sllibrary")
    @ResponseFormat(type = "withTotal")
    //@Paging(limit = 5)
    Object getSlList(OAuth2Authentication oAuth2Authentication){
        String uid = this.getUid(oAuth2Authentication);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        int start, limit;
        // int limit
        String reqStart = request.getParameter("skip");
        String reqLimit = request.getParameter("limit");
        return sllibraryService.getList(Integer.parseInt(uid),reqStart,reqLimit);
    }

    @PostMapping("/sllibrary/test")
    @ResponseFormat(type="withSingle")
    Object newTraining(OAuth2Authentication oAuth2Authentication,HttpServletRequest request){
        String uid = this.getUid(oAuth2Authentication);
        String libraryId = request.getParameter("library_id");
        return sllibraryService.makeTraining(Integer.parseInt(uid),Integer.parseInt(libraryId));
    }

    @PostMapping("/sllibrary/answer")
    @ResponseFormat(type="withSingle")
    Object answer(@Valid SlSimulibraryAnswerDto slSimulibraryAnswerDto, BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        return sllibraryService.answer(slSimulibraryAnswerDto);
    }

    @RequestMapping("/sllibrary/simu")
    @ResponseFormat(type="withSingle")
    Object getSimuDetail(OAuth2Authentication oAuth2Authentication,HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
//        JSONObject o = new JSONObject();
//        o.put("resources",sllibraryService.getMaterial(Integer.parseInt(id)));
//        o.put("questions",sllibraryService.getQuestion(Integer.parseInt(id)));
//        return o;
        return sllibraryService.getMaterialAndQuestion(Integer.parseInt(id));
    }
    @RequestMapping("/sllibrary/simulist")
    @ResponseFormat(type = "withTotal")
    @Paging(limit = 10)
    Object getSimuList(OAuth2Authentication oAuth2Authentication,HttpServletRequest request) {
        String uid = this.getUid(oAuth2Authentication);
        return slSimuService.getByUserId(Integer.parseInt(uid));
    }

    @PostMapping("/slsibrary/simusubmit")
    @ResponseFormat(type = "withSingle")
    Object postSimu(@Valid SlSimulibrarySubmitDto slSimulibrarySubmitDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        slSimulibrarySubmitDto.setUserId(Integer.parseInt(uid));
        return slSimuService.submit(slSimulibrarySubmitDto);
    }

    @PostMapping("/slsibrary/simuuptime")
    @ResponseFormat(type = "withSingle")
    Object uptimeSimu(@Valid SlSimulibraryUpdatetimeDto slSimulibraryUpdatetimeDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        slSimulibraryUpdatetimeDto.setUserId(Integer.parseInt(uid));
        return slSimuService.uptime(slSimulibraryUpdatetimeDto);
    }
}
